﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ACCPS1ProjectDemo.ControlDemo
{
    public partial class DataGridViewDemo : UserControl
    {
        public DataGridViewDemo()
        {
            InitializeComponent();
        }

        DBHelper helper = new DBHelper();

        ///加载学生
        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "select studentNo,studentName,gradeName,phone,address from student as s inner join grade as g on s.gradeId=g.gradeId";
            DataTable table = helper.ExectueTable(sql);
            dataGridView1.DataSource = table;
        }

        /// <summary>
        /// 获取选中的学生
        /// </summary>
        private void button2_Click(object sender, EventArgs e)
        {
            string msg = "共选中行数：" + dataGridView1.SelectedRows.Count+"\n";

            if (dataGridView1.SelectedRows.Count>0) //判断是否选中一行
            {
                for(int i=0;i<dataGridView1.SelectedRows.Count;i++)
                {
                    msg += string.Format("学号：{0}，姓名：{1}，年级：{2}，电话：{3}，地址：{4}\n",
                        dataGridView1.SelectedRows[i].Cells["Column1"].Value, //根据列名获取值
                        dataGridView1.SelectedRows[i].Cells["Column2"].Value,
                        dataGridView1.SelectedRows[i].Cells[2].Value,  //根据列索引获取值
                        dataGridView1.SelectedRows[i].Cells["Column4"].Value,
                        dataGridView1.SelectedRows[i].Cells["Column5"].Value
                    );
                }
                MessageBox.Show(msg);
            }
            
        }
        /// <summary>
        /// 突显学生
        /// </summary>
        private void button3_Click(object sender, EventArgs e)
        {
            //注意：实现该功能不能启用DataGridView控件的“添加”功能

            //循环DataGridView控件中的每一行
            foreach(DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Cells[2].Value.ToString()=="S1") //如果第3列的值为“S1”
                {
                    row.DefaultCellStyle.BackColor = Color.Yellow; //背景颜色
                    row.DefaultCellStyle.ForeColor = Color.Red;  //字体颜色
                }
            }
        }

        /// <summary>
        /// 隔行换色
        /// </summary>
        private void button4_Click(object sender, EventArgs e)
        {
            /*实现隔行换色方法有两种：
             * 方法1. 直接设置DataGridView控件的属性：AlternatingRowsDefaultCellStyle  (交替行的样式）
             * 方法2. 如下代码所示
             */
           
            //循环DataGridView控件中的每一行
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Index%2==0) //如果是偶数行
                {
                    row.DefaultCellStyle.BackColor = Color.Gray; //背景颜色
                }
            }
        }

        /// <summary>
        /// 选中内容发生改变
        /// </summary>
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if(dataGridView1.SelectedRows.Count==1)
            {
                label1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            }
        }
    }
}
